package com.ruoyi.dev.controller;

import cn.hutool.db.Entity;
import cn.hutool.db.Page;
import cn.hutool.db.PageResult;
import cn.hutool.db.sql.Direction;
import cn.hutool.db.sql.Order;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.dev.domain.DevDataset;
import com.ruoyi.dev.service.IDevChartService;
import com.ruoyi.dev.service.IDevModelDataService;
import com.ruoyi.dev.vo.DbWhere;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 模型数据Controller
 *
 * @author yepanpan
 * @date 2025-01-21
 */
@RestController
@RequestMapping("/dev/data/")
public class DevModelDataController extends BaseController
{
    @Autowired
    private IDevModelDataService devModelDataService;

    @Autowired
    private IDevChartService devChartService;

    /**
     * 查询数据集的数据
     */
    @GetMapping("/ds/{code}")
    public AjaxResult dsQuery(@PathVariable("code") String dsCode, Map<String, Object> params)
    {
        DevDataset devDataset = new DevDataset();
        devDataset.setCode(dsCode);
        devDataset.setParams(params);
        return success(devModelDataService.queryDs(devDataset));
    }

    /**
     * 查询报表信息
     */
    @GetMapping("/chart/{code}")
    public AjaxResult chartInfo(@PathVariable("code") String chartCode)
    {
        return success(devChartService.selectDevChartByCode(chartCode));
    }

    /**
     * 查询模型数据列表
     */
    @GetMapping("/list/{code}")
    public TableDataInfo list(@PathVariable("code") String modelCode, DbWhere params)
    {
        //分页
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        Page page = new Page(pageNum-1, pageSize);
        if(StringUtils.isNotEmpty(pageDomain.getOrderByColumn())){
            page.addOrder(new Order(pageDomain.getOrderByColumn(), Direction.fromString(pageDomain.getIsAsc())));
        }

        //查询
        PageResult<Entity> result = devModelDataService.selectDevModelDataList(modelCode, params, page);

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(result);
        rspData.setTotal(result.getTotal());
        return rspData;
    }

    /**
     * 导出模型数据列表
     */
    @PostMapping("/export/{code}")
    public void export(@PathVariable("code") String modelCode, HttpServletResponse response, DbWhere params)
    {
        devModelDataService.exportExcel(response, modelCode, params);
    }

    @PostMapping("/importData/{code}")
    public AjaxResult importData(@PathVariable("code") String modelCode, MultipartFile file, boolean updateSupport) throws Exception
    {
        String operName = getUsername();
        String message = devModelDataService.importData(modelCode,file.getInputStream(), updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate/{code}")
    public void importTemplate(@PathVariable("code") String modelCode, HttpServletResponse response)
    {
        devModelDataService.importTemplateExcel(response, modelCode);
    }

    /**
     * 获取模型数据详细信息
     */
    @GetMapping(value = "/info/{code}/{id}")
    public AjaxResult getInfo(@PathVariable("code") String modelCode, @PathVariable("id") Long id)
    {
        return success(devModelDataService.selectDevModelDataById(modelCode, id));
    }

    /**
     * 新增模型数据
     */
    @PreAuthorize("@ss.hasPermi('dev:model:add')")
    @Log(title = "模型数据", businessType = BusinessType.INSERT)
    @PostMapping("/add/{code}")
    public AjaxResult add(@PathVariable("code") String modelCode, @RequestBody Map<String, Object> data)
    {
        return toAjax(devModelDataService.insertDevModelData(modelCode, data));
    }

    /**
     * 修改模型数据
     */
    @PutMapping("/update/{code}")
    public AjaxResult edit(@PathVariable("code") String modelCode, @RequestBody Map<String, Object> data)
    {
        return toAjax(devModelDataService.updateDevModelData(modelCode, data));
    }

    /**
     * 删除模型数据
     */
    @PreAuthorize("@ss.hasPermi('dev:model:remove')")
    @Log(title = "模型数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/del/{code}/{ids}")
    public AjaxResult remove(@PathVariable("code") String modelCode, @PathVariable Long[] ids)
    {
        return toAjax(devModelDataService.deleteDevModelDataByIds(modelCode, ids));
    }
}
